package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.dashboard.bean.AdFolder;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdFolderService;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/common/folder")
public class AdFolderController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdFolderController.class);

    @Reference
    private AdFolderService adFolderService;

    @RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse get(HttpServletRequest request) {
        Integer folderType = Integer.parseInt(request.getParameter("folderType"));

        List<AdFolder> folders = adFolderService.selectAdFoldersByType(folderType);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.listToListMap(folders));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse add(HttpServletRequest request) {
        String folderName = request.getParameter("folderName");
        Integer folderParent = Integer.parseInt(request.getParameter("folderParent"));
        Integer folderType = Integer.parseInt(request.getParameter("folderType"));

        AdFolder adFolder = new AdFolder();
        adFolder.setFolderName(folderName);
        adFolder.setFolderParent(folderParent);
        adFolder.setFolderType(folderType);

        adFolder.setFolderId(adFolderService.insert(adFolder));
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adFolder));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse update(HttpServletRequest request) {
        Integer folderId = Integer.parseInt(request.getParameter("folderId"));
        String folderName = request.getParameter("folderName");

        AdFolder adFolder = new AdFolder();
        adFolder.setFolderId(folderId);
        adFolder.setFolderName(folderName);

        adFolderService.updateByPrimaryKeySelective(adFolder);

        adFolder = adFolderService.selectByPrimaryKey(folderId);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adFolder));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse delete(HttpServletRequest request) {
        Integer folderId = Integer.parseInt(request.getParameter("folderId"));
        adFolderService.deleteByPrimaryKey(folderId);
        return JsonResponse.success();
    }
}
